<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // https://leetcode-cn.com/problems/maximum-subarray/
        var maxSubArray = function(nums) {
            let sum = nums[0];
            let max = nums[0];
            for (let i = 1; i < nums.length; i++) {
                // 小于 0 的值 直接舍去
                if (sum < 0) {
                    sum = nums[i];
                } else {
                    sum += nums[i];
                }
                max = max < sum ? sum : max
            }
            return max;
        };
    </script>
    <script>
        // 动态规划解决
        // 先感性的认识一下动态规划
        // 题解
        // https://leetcode-cn.com/problems/maximum-subarray/solution/dong-tai-gui-hua-fen-zhi-fa-python-dai-ma-java-dai/
        var maxSubArray2 = function(nums) {
            let dp = [];
            dp[0] = nums[0];
            for (let i = 1; i < nums.length; i++) {
                if (dp[i - 1] <= 0) {
                    dp[i] = nums[i];
                } else {
                    dp[i] = dp[i - 1] + nums[i];
                }
            }
            return dp.sort((a, b) => {
                return b - a
            })[0]
        };
    </script>
</body>
</html>